package cz.via.wowss.service;

import java.util.ArrayList;
import java.util.List;

import cz.via.wowss.dao.CompanyDao;
import cz.via.wowss.dao.GameDao;
import cz.via.wowss.dao.TransactionDao;
import cz.via.wowss.entities.Company;
import cz.via.wowss.entities.CompanyHistory;
import cz.via.wowss.entities.News;
import cz.via.wowss.entities.PlayerTransaction;
import cz.via.wowss.entities.Shareholder;

public class DatabaseService {

	public List<News> getNews(int offset) {
		List<News> news;

		CompanyDao cd = new CompanyDao();
		
		news = cd.getNews(offset);
		
		return news;
	}
	
	public List<News> getNewsByType(int offset, String companyName) {
		List<News> news;

		CompanyDao cd = new CompanyDao();
		Company c = cd.getCompany(companyName);
		
		news = cd.getNewsByType(offset, c.getType());
		
		return news;
	}

	public List<Shareholder> getShares(String playerName) {
		return new CompanyDao().getShares(new GameDao().getPlayerId(playerName));
	}

	public boolean buy(int userId, String what, int count) {

		TransactionDao td = new TransactionDao();
		CompanyDao cd = new CompanyDao();
		
		Company c = cd.getCompany(what);
		
		boolean success = td.buyShare(c, count, userId);

		return success;
	}

	public boolean sell(int userId, String what, int count) {
		TransactionDao td = new TransactionDao();
		CompanyDao cd = new CompanyDao();
		
		Company c = cd.getCompany(what);
		
		boolean success = td.sellShare(c, count, userId);
		
		return success;
	}

	public List<Company> getCompanies(String order, String filter,
			int maxResults, int offset) {
		
		CompanyDao cd = new CompanyDao();
		
		List<Company> companies = cd.getCompanies();
		// TODO: Getnout firmy z DB dle poradi, filtru, poctu vysledku a offsetu
		
		return companies;
	}

	public Company getCompany(String companyName) {
		return new CompanyDao().getCompany(companyName);
	}

	public List<CompanyHistory> getHistory(String companyName, String from,
			String to) {
		return new CompanyDao().getHistory(new CompanyDao().getCompany(companyName).getId(), from, to);
	}

	public List<PlayerTransaction> getPlayerTransactions(String playerName,
			String from, String to) {
		List<PlayerTransaction> transactions = new ArrayList<PlayerTransaction>();
		// TODO: Getnout transakce hrace od:do
		return transactions;
	}
//
//	private Company getSampleCompany() {
//		Company c = new Company();
//
//		c.setName("Kvak");
//		c.setId(0);
//		c.setStock(100);
//		c.setStockLeft(50);
//
//		CompanyType t = new CompanyType();
//		t.setId(0);
//		t.setName("Kvok");
//		c.setType(t.getId());
//
//		c.setValue(new BigDecimal("18.88"));
//
//		return c;
//	}

	public double getPlayerMoney(String name) {
		GameDao gd = new GameDao();
		return gd.getPlayerMoney(name);
	}
}
